package cn.doctor.assistant.pad.api.ziliao;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.android.volley.Response;

import java.util.List;

import cn.doctor.assistant.pad.api.bean.AssResponse;
import cn.doctor.assistant.pad.api.AssistAPI;
import cn.doctor.assistant.pad.api.bean.ReqBean;
import cn.doctor.assistant.pad.base.AssJsonRequest;
import cn.doctor.assistant.pad.base.ResultCallback;
import cn.doctor.assistant.pad.entity.Course;
import cn.doctor.assistant.pad.entity.Dept;
import cn.doctor.assistant.pad.entity.Doctor;
import cn.doctor.assistant.pad.entity.PageInfo;
import cn.doctor.assistant.pad.entity.Patient;
import cn.doctor.assistant.pad.entity.Record;

/**
 * Created by jiaolf on 2016/3/24.
 * 资料
 */
public class InformatApi {
    // 病人资料
    public static final String METHOD_GET_PAITENT = "GetPatient";
    public static final String METHOD_SEARCH_PAITENT = "SearchPaitent";
    public static final String METHOD_ADD_PATIENT = "AddPaitent";

    // 病案资料 TODO
    public static final String METHOD_GET_RECORD = "GetHospitalRecord";
    public static final String METHOD_ADD_RECORD = "AddHospitalRecord";

    // 科室列表、医生列表
    public static final String METHOD_DEPT_LIST = "DatasourceDept";
    public static final String METHOD_DOCTOR_LIST = "DatasourceDoctor";

    // 病程资料 TODO
    public static final String METHOD_GET_BINGCHENG = "GetCourse";
    public static final String METHOD_ADD_BINGCHENG = "AddCourse";

    /**
     * 获取病人列表
     * @param currentPage 当前页
     * @param pageSize 每页显示数量
     * @param callback
     */
    public static void getPatients(int currentPage, int pageSize, final ResultCallback<PatientResponse> callback) {
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageSize(pageSize);
        pageInfo.setCurrPage(currentPage);

        PatientRequest request = new PatientRequest();
        request.setPageInfo(pageInfo);

        ReqBean reqBean = ReqBean.newInstance(METHOD_GET_PAITENT, request);
        String jsonParams = JSON.toJSONString(reqBean);

        AssJsonRequest jsonRequest = new AssJsonRequest(jsonParams, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("jzsn", response);
                AssResponse resp = JSON.parseObject(response, AssResponse.class);
                if (resp.isSuccess()) {
                    PatientResponse patientResponse = new PatientResponse();

                    patientResponse.setCurrentPage(JSON.parseObject(resp.getJsonValue("currentPage"), Integer.class));
                    patientResponse.setPageTotal(JSON.parseObject(resp.getJsonValue("pageTotal"), Integer.class));
                    patientResponse.setTotalCount(JSON.parseObject(resp.getJsonValue("totalCount"), Integer.class));
                    patientResponse.setPaitents(JSON.parseArray(resp.getJsonValue("paitents"), Patient.class));

                    callback.onSuccess(patientResponse);
                }else{
                    callback.onFail("获取数据失败:"+resp.getResult().getInfo());
                }
            }
        });

        jsonRequest.setTag(METHOD_GET_PAITENT);
        AssistAPI.getRequestQueue().add(jsonRequest);
    }


    /**
     * 搜索病人
     * @param currentPage
     * @param pageSize
     * @param content 搜索关键字，名字或手机号，支持模糊查询
     * @param callback
     */
    public static void searchPatient(int currentPage, int pageSize, String content, final ResultCallback<PatientResponse> callback) {
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageSize(pageSize);
        pageInfo.setCurrPage(currentPage);

        PatientSearchRequest request = new PatientSearchRequest();
        request.setPageInfo(pageInfo);
        request.setContent(content);

        ReqBean reqBean = ReqBean.newInstance(METHOD_SEARCH_PAITENT, request);
        String jsonParams = JSON.toJSONString(reqBean);

        AssJsonRequest jsonRequest = new AssJsonRequest(jsonParams, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("jzsn", response);
                AssResponse resp = JSON.parseObject(response, AssResponse.class);
                if (resp.isSuccess()) {
                    PatientResponse patientResponse = new PatientResponse();

                    patientResponse.setCurrentPage(JSON.parseObject(resp.getJsonValue("currentPage"), Integer.class));
                    patientResponse.setPageTotal(JSON.parseObject(resp.getJsonValue("pageTotal"), Integer.class));
                    patientResponse.setTotalCount(JSON.parseObject(resp.getJsonValue("totalCount"), Integer.class));
                    patientResponse.setPaitents(JSON.parseArray(resp.getJsonValue("paitents"), Patient.class));

                    callback.onSuccess(patientResponse);
                }else{
                    callback.onFail("获取数据失败:"+resp.getResult().getInfo());
                }
            }
        });

        jsonRequest.setTag(METHOD_SEARCH_PAITENT);
        AssistAPI.getRequestQueue().add(jsonRequest);

    }

    /**
     * 新增病人
     * @param patientRequest
     * @param callback
     */
    public static void addPatient(AddPatientRequest patientRequest, final ResultCallback<String> callback) {
        ReqBean reqBean = ReqBean.newInstance(METHOD_ADD_PATIENT, patientRequest);
        String jsonStrParmas = JSON.toJSONString(reqBean);

        AssJsonRequest jsonRequest = new AssJsonRequest(jsonStrParmas, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                callback.onSuccess(response);
            }
        });

        jsonRequest.setTag(METHOD_ADD_PATIENT);
        AssistAPI.getRequestQueue().add(jsonRequest);
    }

    /**
     * 获取病案列表
     * @param currentPage
     * @param pageSize
     * @param callback
     */
    public static void getRecords(int currentPage, int pageSize, final ResultCallback<RecordResponse> callback) {
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageSize(pageSize);
        pageInfo.setCurrPage(currentPage);

        RecordRequest request = new RecordRequest();
        request.setUserId("");
        request.setPageInfo(pageInfo);

        ReqBean reqBean = ReqBean.newInstance(METHOD_GET_RECORD, request);
        String jsonParams = JSON.toJSONString(reqBean);

        AssJsonRequest jsonRequest = new AssJsonRequest(jsonParams, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("jzsn", response);
                AssResponse resp = JSON.parseObject(response, AssResponse.class);
                if (resp.isSuccess()) {
                    RecordResponse recordResponse = new RecordResponse();

                    recordResponse.setCurrentPage(JSON.parseObject(resp.getJsonValue("currentPage"), Integer.class));
                    recordResponse.setPageTotal(JSON.parseObject(resp.getJsonValue("pageTotal"), Integer.class));
                    recordResponse.setTotalCount(JSON.parseObject(resp.getJsonValue("totalCount"), Integer.class));
                    recordResponse.setRecords(JSON.parseArray(resp.getJsonValue("recordList"), Record.class));

                    callback.onSuccess(recordResponse);
                }else{
                    callback.onFail("获取数据失败:"+resp.getResult().getInfo());
                }
            }
        });

        jsonRequest.setTag(METHOD_GET_RECORD);
        AssistAPI.getRequestQueue().add(jsonRequest);
    }

    /**
     * 根据userId获取病人的病案资料
     * @param uid
     * @param currentPage
     * @param pageSize
     * @param callback
     */
    public static void getRecordWithUid(String uid, int currentPage, int pageSize, final ResultCallback<RecordResponse> callback){
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageSize(pageSize);
        pageInfo.setCurrPage(currentPage);

        RecordRequest request = new RecordRequest();
        request.setUserId(uid);
        request.setPageInfo(pageInfo);

        ReqBean reqBean = ReqBean.newInstance(METHOD_GET_RECORD, request);
        String jsonParams = JSON.toJSONString(reqBean);

        AssJsonRequest jsonRequest = new AssJsonRequest(jsonParams, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("jzsn", response);
                AssResponse resp = JSON.parseObject(response, AssResponse.class);
                if (resp.isSuccess()) {
                    RecordResponse recordResponse = new RecordResponse();

                    recordResponse.setCurrentPage(JSON.parseObject(resp.getJsonValue("currentPage"), Integer.class));
                    recordResponse.setPageTotal(JSON.parseObject(resp.getJsonValue("pageTotal"), Integer.class));
                    recordResponse.setTotalCount(JSON.parseObject(resp.getJsonValue("totalCount"), Integer.class));
                    recordResponse.setRecords(JSON.parseArray(resp.getJsonValue("recordList"), Record.class));

                    callback.onSuccess(recordResponse);
                }else{
                    callback.onFail("获取数据失败:"+resp.getResult().getInfo());
                }
            }
        });

        jsonRequest.setTag(METHOD_GET_RECORD);
        AssistAPI.getRequestQueue().add(jsonRequest);
    }

    /**
     * 获取所在医院的科室列表
     * @param callback
     */
    public static void getDeptList(final ResultCallback<List<Dept>> callback) {
        ReqBean reqBean = ReqBean.newInstance(METHOD_DEPT_LIST, null);
        String jsonParams = JSON.toJSONString(reqBean);

        AssJsonRequest jsonRequest = new AssJsonRequest(jsonParams, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("jzsn", response);
                AssResponse resp = JSON.parseObject(response, AssResponse.class);
                if (resp.isSuccess()) {
                    //XXX dept? dpet ?
                    List<Dept> list = JSON.parseArray(resp.getJsonValue("dpet_list"), Dept.class);
                    callback.onSuccess(list);
                }else{
                    callback.onFail("获取数据失败:"+resp.getResult().getInfo());
                }
            }
        });

        jsonRequest.setTag(METHOD_DEPT_LIST);
        AssistAPI.getRequestQueue().add(jsonRequest);
    }

    /**
     * 根据科室获取对应的医生列表
     * @param dept_id
     * @param callback
     */
    public static void getDoctorList(int dept_id, final ResultCallback<List<Doctor>> callback) {
        DeptDoctorRequest doctorRequest = new DeptDoctorRequest();
        doctorRequest.setDept_id(dept_id);

        ReqBean reqBean = ReqBean.newInstance(METHOD_DOCTOR_LIST, doctorRequest);
        String jsonParams = JSON.toJSONString(reqBean);

        AssJsonRequest jsonRequest = new AssJsonRequest(jsonParams, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("jzsn", response);
                AssResponse resp = JSON.parseObject(response, AssResponse.class);
                if (resp.isSuccess()) {
                    List<Doctor> list = JSON.parseArray(resp.getJsonValue("doctor_list"), Doctor.class);
                    callback.onSuccess(list);
                }else{
                    callback.onFail("获取数据失败:"+resp.getResult().getInfo());
                }
            }
        });

        jsonRequest.setTag(METHOD_DOCTOR_LIST);
        AssistAPI.getRequestQueue().add(jsonRequest);
    }

    /**
     * 新增病案
     * @param addRecordRequest
     * @param callback
     */
    public static void addRecord(AddRecordRequest addRecordRequest, final ResultCallback<String> callback) {
        ReqBean reqBean = ReqBean.newInstance(METHOD_ADD_RECORD, addRecordRequest);
        String jsonStrParams = JSON.toJSONString(reqBean);

        AssJsonRequest jsonRequest = new AssJsonRequest(jsonStrParams, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                callback.onSuccess(response);
            }
        });

        jsonRequest.setTag(METHOD_ADD_RECORD);
        AssistAPI.getRequestQueue().add(jsonRequest);
    }

    /**
     * 获取病程
     * @param callback
     */
    public static void getBingCheng(int record_id, final ResultCallback<List<Course>> callback) {
        CourseRequest courseRequest = new CourseRequest();
        courseRequest.setRecord_id(record_id);

        ReqBean reqBean = ReqBean.newInstance(METHOD_GET_BINGCHENG, courseRequest);
        String jsonStrParams = JSON.toJSONString(reqBean);

        AssJsonRequest jsonRequest = new AssJsonRequest(jsonStrParams, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                AssResponse resp = JSON.parseObject(response, AssResponse.class);
                List<Course> list = JSON.parseArray(resp.getJsonValue("course"), Course.class);
                callback.onSuccess(list);
            }
        });

        jsonRequest.setTag(METHOD_GET_BINGCHENG);
        AssistAPI.getRequestQueue().add(jsonRequest);
    }

    /**
     * 添加病程
     * @param course
     * @param callback
     */
    public static void addBingCheng(Course course, final ResultCallback<String> callback) {
        ReqBean reqBean = ReqBean.newInstance(METHOD_ADD_BINGCHENG, course);
        String jsonStrParams = JSON.toJSONString(reqBean);

        AssJsonRequest jsonRequest = new AssJsonRequest(jsonStrParams, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                callback.onSuccess("添加成功");
            }
        });

        jsonRequest.setTag(METHOD_ADD_BINGCHENG);
        AssistAPI.getRequestQueue().add(jsonRequest);

    }
}
